package com.example.demo.repository;

import com.example.demo.entity.OrdersItem;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.persistence.LockModeType;
import java.util.List;

public interface OrdersItemRepository extends JpaRepository<OrdersItem, Long>, JpaSpecificationExecutor<OrdersItem> {

    List<OrdersItem> findByOrderId(Long id);

    @Query(value = "select count(id) from orders_item where order_id = :orderId and complete = 0", nativeQuery = true)
    long unConfirmCount(@Param("orderId") Long orderId);

    @Lock(LockModeType.PESSIMISTIC_WRITE)
    @Query("from OrdersItem i where i.id = :id")
    OrdersItem select4update(@Param("id") Long id);
}